package model;

import java.util.List;

import model.Order.OrderLine;
import model.Repair.RepairLine;

import DAO.Dao;

/**
 * The Service class has methods that add and remove model objects from containers.
 * It can also have methods containing business logic.
 * @author Bogdan
 */
public class Service {

	private static Service instance = new Service();
	private Dao dao = new Dao();

	Service() {
			startUp();
	}

//	----------------------------------------------------------------------------
	/**
	 * Returns the one and only instance of the Service class.
	 */
	public static Service getInstance() {
		return instance;
	}

	public List<SparePart> returnParts() {
		return dao.returnParts();
	}

	public List<Machine> returnMachines() {
		return dao.returnMachines();
	}

	public List<Repair> returnRepairs() {
		return dao.returnRepairs();
	}

	public List<StorageArea> returnDrawers() {
		return dao.returnDrawers();
	}

	public List<Order> returnOrders() {
		return dao.returnOrders();
	}

	//--------------------------------------------------------------------------

	public void createMachine(int code, String model, String manufacturer,
			String manual, int sketch) {
		dao.createMachine(code, model, manufacturer, manual, sketch);
	}

	public void createSparePart(int ID, StorageArea drawer, String sketch,
			int minimumAmount, int actualAmount) {
		dao.createSparePart(ID, drawer, sketch, minimumAmount, actualAmount);
	}

	public void createRepair(Machine machine) {
		dao.createRepair(machine);
		machine.setState(false);
		//Assumption: That when we create a new repair it is going to change the
		//of the machine.
	}
	/**
	 * Not sure about this one !
	 * @param repair
	 */
	public void endRepair(Repair repair){
		//enddate stuff I dunno' didn't add 
		repair.getMachine().setState(true);
	}

	public void createStorage(int id) {
		dao.createStorage(id);
	}

	public void createOrder(String title) {
		dao.createOrder(title);
	}

	//--------------------------------------------------------------------------

	/**
	 * Delete a Machine from the Service.
	 * 	Requires: Machine != null 
	 * @author George
	 */
	public void deleteMachine(Machine m) {
		dao.deleteMachine(m);
	}

	/**
	 * Delete a SparePart from the Service.
	 * 	Requires: SparePart != null 
	 * @author George
	 */
	public void deleteSparePart(SparePart sp) {
		dao.deleteSparePart(sp);
	}

	public void removeRepair(Repair r) {
		dao.removeRepair(r);
	}

	/**
	 * Removes a StorageArea from the Service.
	 * 	Requires: StorageArea != null 
	 * @author George
	 */
	public void removeStorage(StorageArea sa) {
		dao.removeStorage(sa);
	}

	/**
	 * Removes a Order from the Service.
	 * 	Requires: Order != null 
	 * @author George
	 */
	public void removeOrder(Order o) {
		dao.removeOrder(o);
	}

	//----------------------------------------------------------------------------
	/**
	 * It must add new spare part to the machine.
	 * @author George
	 */
	public void registerMachineLine(SparePart part, Machine machine) {
		dao.addSparePartToMachine(part, machine);
	}

	//--------------------------------------------------------------------------

	/**
	 * Should register the used spare part it to the repair.
	 * @author George
	 */
	public void registerRepairLine(SparePart part, int amount, Repair repair) {
		repair.createRepairLine(part, amount);
	}

	public void registerOrderLine(SparePart part, int amount, Order order) {
		dao.registerOrderLine(part, amount, order);
	}

	public void updateOrderLine(SparePart part, int amount, Order order,
			OrderLine orderline) {
		dao.updateOrderLine(part, amount, order, orderline);
	}

	public void updateMachine(int code, String model, String manufacturer,
			String manualLocation, int sketch, Machine machine) {
		dao.updateMachine(code, model, manufacturer, manualLocation, sketch,
				machine);
	}

	public void updateRepair(Machine machine, Repair repair) {
		dao.updateRepair(machine, repair);
	}

	public void updateRepairLine(SparePart part, int amount, Repair repair,
			RepairLine repairLine) {
		dao.updateRepairLine(part, amount, repair, repairLine);
	}

	public void updateOrder(String title, Order order) {
		dao.updateOrder(title, order);
	}

	public void updateStorageArea(int id, StorageArea storage) {
		dao.updateStorageArea(id, storage);
	}

	//-------------------------------------------------------------------------
	public void searchSparePart(int idNumber) {
		dao.searchSparePart(idNumber);
	}

	public Machine searchMachine(int code) {
		return dao.searchMachine(code);
	}
	
	//------------------------------------------------------------------------
	
	public void changePassword(String user, String pass){
		dao.changePassword(user, pass);
	}
	public boolean login(String user, String pass){
		return dao.login(user, pass);
	}
	public void createAccount(String username, String password){
		dao.createAccount(username, password);
	}
	
//-----------------------------------------------------------------------
	public void startUp(){
		createAccount("Inventory Manager", "1");
		createAccount("Erector", "b");
		createMachine(123, "Z4000", "Nokia", "Page 200", 1200);
		createMachine(124, "N6300", "SIEMENS", "Page 201", 1500);
		createMachine(122, "Z4", "mICrosoFt", "Page 202", 1400);
		createMachine(121, "XXX", "Nokia", "Page 203", 1300);
		createRepair(returnMachines().get(0));
		StorageArea dr1 = new StorageArea(90);
		createStorage(90);
		for ( int i = 91; i<= 101;i++){
			createStorage(i);
		}
		createSparePart(101, dr1, null, 100, 150);
		createSparePart(102, returnDrawers().get(4), null, 100, 350);
		createSparePart(103, returnDrawers().get(6), null, 150, 550);
		createSparePart(104, returnDrawers().get(2), null, 100, 1050);
		createSparePart(105, returnDrawers().get(7), null, 20, 150);
		//createRepair(returnMachines().get(0));
		createOrder("Order #1");
		returnOrders().get(0).createOrderLine(100, returnParts().get(0));
		createOrder("Order #2");
		returnRepairs().get(0).createRepairLine(returnParts().get(2), 15);
		returnMachines().get(0).addSparePart(returnParts().get(2));
		returnMachines().get(0).addSparePart(returnParts().get(1));
		returnMachines().get(0).addSparePart(returnParts().get(0));

	}
	
}
